package com.yan.furns.web.servlet;

import com.yan.furns.entity.Furn;
import com.yan.furns.entity.Page;
import com.yan.furns.service.FurnService;
import com.yan.furns.service.impl.FurnServiceImpl;
import com.yan.furns.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Yzb
 * @version 1.0
 */
public class CustomerFurnServlet extends BasicServlet {
	FurnService furnService = new FurnServiceImpl();
	protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
		Integer pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);

		Page<Furn> page = furnService.queryFurnPage(pageNo, pageSize);
		req.setAttribute("page", page);
		req.getRequestDispatcher("/views/customer/index.jsp")
				.forward(req, resp);
	}

	protected void pageByName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Integer pageSize = DataUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
		Integer pageNo = DataUtils.parseInt(req.getParameter("pageNo"), 1);
		String name = req.getParameter("name");
		if (name == null) {
			name = "";
		}

		StringBuilder url = new StringBuilder("customerFurnServlet?action=pageByName");
		if (!name.isEmpty()) {
			url.append("&name=").append(name);
		}

		Page<Furn> page = furnService.queryFurnPageByName(pageNo, pageSize, name);
		page.setUrl(url.toString());
		req.setAttribute("page", page);
		req.getRequestDispatcher("/views/customer/index.jsp")
				.forward(req, resp);
	}
}
